home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
apps
/
other
/
am.zoo
/
am.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-08
|
20KB
|
754 lines
/************************************************************************/
/* */
/* am - Amortization Schedule Generator */
/* */
/* (c) Copyright 1987, 1992, 1993 Brett K. Carver */
/* */
/************************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "patchlevel.h"
/* these two constants may be changed at will */
#define ARRAY_SIZE 128 /* special arrays size: -I, -E, -U */
#define SEP '-' /* seperator for dates: 1/1/93 or 1-1-93*/
/* these constants should not be changed */
#define FIELD_LENGTH 15 /* how wide are numbers: 123,456,789.12 */
#define MAX_VAL 999999999.99 /* maximum dollar amount allowed */
#define S_LEN 100 /* length of input buffer string */
#define PLURAL(x) (x)!=1?"s":"" /* adds an 's' for plural items */
#ifndef TRUE
# define TRUE 1
# define FALSE 0
#endif
int number; /* number of payments */
double amount; /* amount of loan */
double rate; /* interest rate */
double payment; /* amount of monthly payment */
int month, day, year; /* starting date */
int e_idx = 0; /* index into extra payment arrays */
int e_mon[ARRAY_SIZE]; /* months of extra payments */
double e_pay[ARRAY_SIZE]; /* amounts of extra payments */
int u_idx = 0; /* index into unusual payment arrays */
int u_mon[ARRAY_SIZE]; /* months of unusual payments */
double u_pay[ARRAY_SIZE]; /* amounts of unusual payments */
int i_idx = 0; /* index into interest rate arrays */
int i_mon[ARRAY_SIZE]; /* months of interest rates */
double i_pay[ARRAY_SIZE]; /* amounts of interest rates */
int list; /* months to list for */
int delta_day; /* first month 30 +/- delta days */
int balloon = FALSE; /* last payment is a balloon payment */
int format = FALSE; /* include nroff formatting commands */
char istr[S_LEN]; /* input buffer string */
#define def_number 360 /* default number of payments */
#define def_amount 10000.0 /* default amount of loan */
#define def_rate 10.0 /* default interest rate */
#define def_payment 100.0 /* default amount of monthly payment */
#define def_start "1-1-90"/* default starting date */
#define def_list 0 /* default months to list for */
char * usage_text[] = {
"\t-n\tnumber of payments (-n-30 is 30 years, 360 months):",
"\t\t\t=0 - till paid in full",
"\t\t\t>0 - number of months",
"\t\t\t<0 - number of years",
"\t-a\tamount of loan (<= $999,999,999.99)",
"\t-i\tinterest rate (-i10 is 10%)",
"\t-p\tmonthly payment amount:",
"\t\t\t=0 - computed based on number of payments",
#if (SEP == '-')
"\t-s\tstart first payment on mm-dd-yy (-s12-15-93 is Dec 15 1993)",
#else
"\t-s\tstart first payment on mm/dd/yy (-s12/15/93 is Dec 15 1993)",
#endif
"\t-l\tlist loan for:",
"\t\t\t=0 - list all payments",
"\t\t\t>0 - number of months",
"\t\t\t<0 - number of years",
"\t-b\tmake last payment a balloon payment if ballance due",
"\t-I\tmonth:rate for new interest rate[s] (up to 128 allowed):",
"\t\t\t>0 - new interest rate (-e8:12 rate at 12% on month 8)",
"\t-E\tmonth:amount for extra payment[s] (up to 128 allowed):",
"\t\t\t>0 - extra principal payment amount (-e7:50 extra $50)",
"\t\t\t<0 - add to unpaid principal (-e5:-100 add $100)",
"\t-U\tmonth:unusual payment[s] (up to 128 allowed):",
"\t\t\t=0 - missed payment (-u9:0 missed payment on month 9)",
"\t\t\t>0 - non-standard payment amount (-u7:75 only paid $75)",
"\t-D\tchange first month by +/- days",
"\t\t\t>0 - month had more days than normal",
"\t\t\t<0 - month had less days than normal",
"\t-f\tOutput with nroff formatting commands",
0 }; /* last entry MUST be null */
char *titles = "NUMBER DATE PAYMENT INTEREST PRINCIPAL BALANCE";
/************************************************************************/
/* */
/************************************************************************/
char * add_commas(value)
double value;
{
static char string[32];
static char * sptr = &string[0];
register int i, j, k;
register int length;
int negative;
negative = FALSE;
if (value < 0) {
value = -value;
negative = TRUE;
}
if (value > MAX_VAL) {
fprintf(stderr, "Error:\ta value has exceeded $999,999,999.99,\n");
fprintf(stderr, "\tplease use another program for calculating the national debt.\n");
exit(1);
}
sprintf(sptr, "%.2f ", value);
length = strlen(sptr); /* sprintf() doesn't return length on all unix's */
length -= 16; /* 1234567890123456 */
length --; /* make 0 relative */
string[FIELD_LENGTH] = '\000';
j = FIELD_LENGTH - 1;
k = 6;
for (i=length; i>=0; i--) {
if ((k--) == 0) {
string[j--] = ',';
k = 2;
}
string[j--] = string[i];
string[i] = ' ';
}
if (negative)
string[j--] = '-';
return(sptr);
}
/************************************************************************/
/* */
/************************************************************************/
int get_int(text, def)
char *text;
int def;
{
int value;
fprintf(stderr, "%s [%d]: ", text, def);
fgets(istr, S_LEN, stdin);
if (strlen(istr) <= 1)
return(def);
value = 0; /* in case nothing scans */
sscanf(istr, "%d", &value);
return(value);
}
/************************************************************************/
/* */
/************************************************************************/
double get_double(text, def)
char *text;
double def;
{
double value;
fprintf(stderr, "%s [%.0f]: ", text, def);
fgets(istr, S_LEN, stdin);
if (strlen(istr) <= 1)
return(def);
value = 0.0; /* in case nothing scans */
sscanf(istr, "%lf", &value);
return(value);
}
/************************************************************************/
/* */
/************************************************************************/
dup_warning(param)
int param;
{
fprintf(stderr, "Warning: duplicate '-%c' parameter\n", param);
}
/************************************************************************/
/* */
/************************************************************************/
init(argc, argv)
int argc;
char *argv[];
{
extern char *optarg;
extern int optind;
int c;
int error_flag = FALSE;
char z[2]; /* a sscanf temp */
int got_number = FALSE;
int got_amount = FALSE;
int got_rate = FALSE;
int got_payment = FALSE;
int got_start = FALSE;
int got_list = FALSE;
int i;
while ((c = getopt(argc, argv, "n:a:i:p:s:l:bI:E:U:D:f?")) != EOF)
switch (c) {
case 'n':
if (got_number)
dup_warning(c);
sscanf(optarg, "%d", &number);
got_number = TRUE;
break;
case 'a':
if (got_amount)
dup_warning(c);
sscanf(optarg, "%lf", &amount);
if (amount <= 0.0) {
fprintf(stderr, "Error: amount <= 0.0\n");
error_flag = TRUE;
}
if (amount > MAX_VAL) {
fprintf(stderr,
"Error: amount > 999,999,999.99\n");
error_flag = TRUE;
}
got_amount = TRUE;
break;
case 'i':
if (got_rate)
dup_warning(c);
sscanf(optarg, "%lf", &rate);
if (rate <= 0.0) {
fprintf(stderr, "Error: rate <= 0.0\n");
error_flag = TRUE;
}
got_rate = TRUE;
break;
case 'p':
if (got_payment)
dup_warning(c);
sscanf(optarg, "%lf", &payment);
if (payment < 0.0) {
fprintf(stderr, "Error: payment < 0.0\n");
error_flag = TRUE;
}
got_payment = TRUE;
break;
case 's':
if (got_start)
dup_warning(c);
sscanf(optarg, "%d%[- /]%d%[- /]%d",
&month, z, &day, z, &year);
got_start = TRUE;
break;
case 'l':
if (got_list)
dup_warning(c);
sscanf(optarg, "%d", &list);
got_list = TRUE;
break;
case 'b':
if (balloon)
dup_warning(c);
balloon = TRUE;
break;
case 'E':
if (e_idx >= ARRAY_SIZE) {
fprintf(stderr, "Error: more than %d extra payments\n",
ARRAY_SIZE);
error_flag = TRUE;
break;
}
sscanf(optarg, "%d:%lf", &e_mon[e_idx], &e_pay[e_idx]);
if (e_mon[e_idx]